package com.bwt.top.bwt_ad_flutter.manager;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import com.bwt.top.bwt_ad_flutter.AdErrorUtil;
import com.bwt.top.bwt_ad_flutter.ad.BwtBannerAd;
import com.bwt.top.bwt_ad_flutter.ad.BwtUnifiedAd;
import com.bwt.top.bwt_ad_flutter.config.BwtAdEventConfig;
import com.bwt.top.bwt_ad_flutter.listener.OnAdReleaseListener;
import com.bwt.top.bwt_ad_flutter.model.AdParams;
import com.bwt.top.bwt_ad_flutter.util.LogUtil;
import com.bwt.top.bwt_ad_flutter.view.factory.BwtBannerAdFactory;
import com.bwt.top.bwt_ad_flutter.view.factory.BwtUnifiedAdFactory;
import com.bwt.top.exception.AdError;

import java.util.HashMap;
import java.util.Map;

public class BwtUnifiedAdManager implements OnAdReleaseListener {

    private static BwtUnifiedAdManager instance;

    private final Map<String, BwtUnifiedAd> bwtUnifiedAdMap = new HashMap<>();


    private BwtUnifiedAdManager(){
        BwtAdManager.getInstance().registerOnAdReleaseListener(this);
    }

    public static BwtUnifiedAdManager getInstance(){
        if (instance == null){
            synchronized (BwtUnifiedAdManager.class){
                if (instance == null){
                    instance = new BwtUnifiedAdManager();
                }
            }
        }
        return instance;
    }

    public void loadUnifiedAd(Context context, @NonNull AdParams adParams,boolean loadThenShow){
        LogUtil.log("信息流自渲染广告加载预备,adId: " + adParams.getAdId());
        if (!(context  instanceof Activity) || ((Activity) context).isFinishing()){
            LogUtil.log("信息流自渲染广告加载失败,adId: "+adParams.getAdId()+"error:当前上下文不适合获取信息流自渲染广告!");
            BwtAdManager.getInstance().postAdEvent(BwtAdEventConfig.KEY_AD_EVENT_ON_AD_FAILED,adParams, AdErrorUtil.getError(new  AdError(-1,"\"当前上下文不适合获取信息流广告\"")));
            return;
        }
        BwtUnifiedAd ad = new BwtUnifiedAd(adParams,(Activity) context);
        bwtUnifiedAdMap.put(adParams.getAdId(),ad);
        ad.loadAd();
    }

    public  void closeAd(String adId){
        if (adId == null) return;
        try {
            BwtUnifiedAd bwtUnifiedAd = bwtUnifiedAdMap.get(adId);
            if (bwtUnifiedAd != null) {

                bwtUnifiedAd.closeAd();

            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    public Map<String, BwtUnifiedAd> getBwtUnifiedAdMap() {
        return bwtUnifiedAdMap;
    }

    @Override
    public void onAdRelease(String adId) {
        if (adId == null) return;
        try {
            BwtUnifiedAd bwtUnifiedAd = bwtUnifiedAdMap.get(adId);
            if (bwtUnifiedAd != null) {
                bwtUnifiedAd.release();
                bwtUnifiedAdMap.remove(adId);
            }
            BwtUnifiedAdFactory factory = BwtAdManager.getInstance().getBwtUnifiedAdFactory();
            if (factory != null) factory.release(adId);
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}
